package cn.blnp.net.flowable.boot.service.impl;

import cn.blnp.net.flowable.boot.ext.cmd.DeleteProcessInstanceCmd;
import cn.blnp.net.flowable.boot.ext.cmd.ReCreateProcessInstanceCmd;
import cn.blnp.net.flowable.boot.service.RevokeProcessInstanceService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ManagementService;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <h3>自定义流程撤销服务实现</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/19 10:45
 */
@Slf4j
@Service
@AllArgsConstructor
public class RevokeProcessInstanceServiceImpl implements RevokeProcessInstanceService {

    protected ManagementService managementService;

    @Override
    public ProcessInstance executeRevoke(String processInstanceId) {
        //实例化删除流程实例Command类
        DeleteProcessInstanceCmd deleteProcessInstanceCmd = new DeleteProcessInstanceCmd(processInstanceId);
        //通过ManagementService管理服务执行删除流程实例的Command类
        Map<String, Object> procInstMap = managementService.executeCommand(deleteProcessInstanceCmd);
        //实例化重建流程实例Command类
        ReCreateProcessInstanceCmd reCreateProcessInstanceCmd = new ReCreateProcessInstanceCmd(procInstMap);
        //通过ManagementService管理服务执行重建流程实例Command类
        ProcessInstance procInst = managementService.executeCommand(reCreateProcessInstanceCmd);
        return procInst;
    }
}
